package com.fsharemobile;




import com.fsharemobile.fragments.ExplorerFragment;
import com.fsharemobile.fragments.PicturesFragment;
import com.fsharemobile.fragments.SettingFragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;


public class HomeActivity extends FragmentActivity implements OnClickListener {
	
	ImageView ivExplorer, ivPicture, ivSetting;
	FragmentManager fragManager;
	FragmentTransaction fragTran;
	
	
	private boolean doubleBackToExitPressedOnce = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ivExplorer= (ImageView) findViewById(R.id.ivExplorer);
        ivPicture= (ImageView) findViewById(R.id.ivPicture);
        ivSetting= (ImageView) findViewById(R.id.ivSetting);
        
        fragManager= getSupportFragmentManager();
        fragTran= fragManager.beginTransaction();
        
        initBackGround();
        
        ivExplorer.setOnClickListener(this);
        ivPicture.setOnClickListener(this);
        ivSetting.setOnClickListener(this);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
			Fragment newFrag = null;
			int id= v.getId();
			if(id== R.id.ivExplorer){
				ivExplorer.setImageResource(R.drawable.ic_home_select);
				ivPicture.setImageResource(R.drawable.ic_view);
		        ivSetting.setImageResource(R.drawable.ic_tool);
				newFrag= new ExplorerFragment();
			}else if(id== R.id.ivPicture){
				ivExplorer.setImageResource(R.drawable.ic_home);
				ivPicture.setImageResource(R.drawable.ic_view_select);
		        ivSetting.setImageResource(R.drawable.ic_tool);
				newFrag= new PicturesFragment();
			}else if(id==R.id.ivSetting){
				ivExplorer.setImageResource(R.drawable.ic_home);
				ivPicture.setImageResource(R.drawable.ic_view);
		        ivSetting.setImageResource(R.drawable.ic_tool_select);
				newFrag= new SettingFragment();
			}
			fragTran = fragManager.beginTransaction();
			fragTran.replace(R.id.layout_fragments, newFrag);
			//fragTran.addToBackStack(null);// lưu lại vết đường đi khi đổi
			fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			fragTran.commit();
		
		
	}


	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		doubleBackToExitPressedOnce= false;
	}


	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		
		 if (doubleBackToExitPressedOnce) {
	            super.onBackPressed();        
	            System.exit(0);
	        }
	        this.doubleBackToExitPressedOnce = true;
	        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
	        new Handler().postDelayed(new Runnable() {

	            @Override
	            public void run() {
	             doubleBackToExitPressedOnce=false;   
	            
	            }
	        }, 2000);
	}
	
	private void initBackGround(){
		
		ExplorerFragment exploreFrag= new ExplorerFragment();
        fragTran.add(R.id.layout_fragments, exploreFrag);
        fragTran.commit();
        
        ivPicture.setImageResource(R.drawable.ic_view);
        ivSetting.setImageResource(R.drawable.ic_tool);
		
	}


	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		Toast.makeText(getApplication(), "dasdas", Toast.LENGTH_LONG).show();
		return super.onTouchEvent(event);
	}
	
	
	
	
    
    
}
